module.exports = (app, OrderUser) => {
  const express = require('express');
  const router = express.Router();
  const Collector = require('../model/collectorOrderModel')

  // 回收员接单
  router.post('/add', async (q, s) => {
    let collector = await Collector.findOne({ collector_phone: q.body.collector_phone });
    if (collector == null) {
      await Collector.create({
        collector_phone: q.body.collector_phone,
        collector_name: q.body.collector_name,
        order_info: [{
          belong_phone: q.body.belong_phone,
          belong_name: q.body.belong_name,
          order_id: q.body.order_id,
          status: 0
        }]
      })
    } else {
      collector.order_info.push({
        belong_phone: q.body.belong_phone,
        belong_name: q.body.belong_name,
        order_id: q.body.order_id,
        status: 0
      })
    }
    let orderUser = await OrderUser.findOne({ belong_phone: q.body.belong_phone });
    let flag = 0;
    orderUser.order_info.forEach((v) => {
      if (v._id == q.body.order_id) {
        v.collector_name = q.body.collector_name,
          v.collector_phone = q.body.collector_phone,
          v.settle_time = q.select_time
        // v.status++;//改变订单状态
        flag = 1;
      }
    });
    if (flag == 1) {
      if (collector != null) {
        await collector.save();
      }
      await orderUser.save();
      s.send({
        success: "true",
        msg: "created",
        data: {
          collector,
          orderUser
        }
      });
    } else {
      s.send({
        success: "false",
        msg: "此订单不存在!",
        data: {}
      })
    }
  })

  // 回收员取消订单
  router.get('/cancel/:collector_phone/:belong_phone/:order_id', async (q, s) => {
    let collector = await Collector.findOne({ collector_phone: q.params.collector_phone });
    if (collector == null) {
      s.send({
        success: "false",
        msg: "此回收员不存在!",
        data: {}
      })
    } else {
      // 找到回收员的订单，改变订单状态
      let flag = 0;
      collector.order_info.forEach((v) => {
        if (v.order_id == q.params.order_id) {
          v.status--;
          flag = 1;
        }
      })
      if (flag == 0) {
        s.send({
          success: "false",
          msg: "此订单不存在!",
          data: {}
        })
        return;
      }
      // 找到用户的订单，改变订单状态
      let orderUser = await OrderUser.findOne({ belong_phone: q.params.belong_phone });
      flag = 0;
      orderUser.order_info.forEach((v) => {
        // console.log(v);
        // console.log(q.params.order_id);
        if (v._id == q.params.order_id) {
          v.status--;
          flag = 1;
        }
      });
      if (flag == 1) {
        await collector.save();
        await orderUser.save();
        s.send({
          success: "true",
          msg: "created",
          data: {
            collector,
            orderUser
          }
        });
      } else {
        s.send({
          success: "false",
          msg: "此订单不存在!",
          data: {}
        })
      }
    }
  })

  app.use('/order/collector', router);
}